package cn.edu.zknu.mita.service.impl;

import cn.edu.zknu.mita.model.FindImage;
import cn.edu.zknu.mita.model.FindMsg;
import cn.edu.zknu.mita.mapper.FindMsgMapper;
import cn.edu.zknu.mita.service.IFindMsgService;
import cn.edu.zknu.mita.service.IFindWallService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.safety.Whitelist;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by iwlnner on 2020/3/26.
 */
@Service
public class FindMsgService implements IFindMsgService {

    @Autowired
    private FindMsgMapper findMsgMapper;
    @Autowired
    private IFindWallService findWallService;


    public PageInfo<FindMsg> listFindMsg(Integer pageNum,Integer pageSize,Integer type){
        PageHelper.startPage(pageNum,pageSize);
        List<FindMsg> findMsgs=findMsgMapper.listFindMsg(type);
        PageInfo<FindMsg> info=new PageInfo<>(findMsgs);
        return info;
    }

    public List<FindMsg> listIndexFindGoodsMsg(){
        return findMsgMapper.listIndexFindGoodsMsg();
    }

    public List<FindMsg> listIndexFindLostorMsg(){
        return findMsgMapper.listIndexFindLostorMsg();
    }

    @Override
    public Long release(FindMsg findMsg) {
        Integer type= findMsg.getType();
        if(type==1){
            findMsg.setCategory("15");
        }
        if(type==2){
            findMsg.setCategory("");
        }
        Document document = Jsoup.parse(findMsg.getDetail());
        Elements imgEles=document.getElementsByTag("img");
        for (Element ele:imgEles){
            ele.attr("style","max-width:600px");
        }
        findMsg.setDetail(document.body().html());
        findMsgMapper.addFindMsg(findMsg);
        this.handleData(findMsg,document);
        return findMsg.getId();
    }

    private void handleData(FindMsg msg,Document document){
        Element imgEle=document.getElementsByTag("img").first();
        while (true){
            if(imgEle!=null){
                if(imgEle.attr("src").contains("/editor/plugins/emoticons/images/")||imgEle.attr("src").contains("http://api.map.baidu.com")){
                    imgEle.remove();
                    imgEle=document.getElementsByTag("img").first();
                    //System.out.println(document.getElementsByTag("img").first());
                }else {
                    findWallService.saveImg(new FindImage(msg.getTitle(),imgEle.attr("src"),msg.getUserId(),msg.getId()));
                    System.out.println("图片保存完成。。。");
                    break;
                }
            }else {
                break;
            }
        }
    }

    @Override
    public FindMsg getDetail(Long id) {
        return findMsgMapper.getDetail(id);
    }


    public List<FindMsg> listMyRelease(Long id){
        return findMsgMapper.listFindMsgByUser(id);
    }

    @Override
    public Integer deleteFindMsg(Long id) {
        return findMsgMapper.deleteFindMsg(id);
    }

    public PageInfo<FindMsg> searchMsg(Integer pageNum, Integer pageSize, String keyword){
        PageHelper.startPage(pageNum,pageSize);
        List<FindMsg> findMsgs=findMsgMapper.listSearchMsg(keyword);
        for (FindMsg findMsg:findMsgs) {
            String html=findMsg.getDetail();
            String textHtml = Jsoup.clean(html, Whitelist.none());
            findMsg.setDetail(textHtml);
        }
        PageInfo<FindMsg> info=new PageInfo<>(findMsgs);
        return info;
    }
}
